package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import com.gulimall.product.dao.SkuInfoDao;
import com.gulimall.product.entity.SkuImagesEntity;
import com.gulimall.product.entity.SkuInfoEntity;
import com.gulimall.product.entity.SpuInfoDescEntity;
import com.gulimall.product.service.*;
import com.gulimall.product.vo.SkuItemInfoVo;
import com.gulimall.product.vo.SpuAttrGroupVo;
import com.gulimall.product.vo.SpuSaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuInfoDao skuInfoDao;

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Autowired
    private ThreadPoolExecutor poolExecutor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(long spuId) {


        List<SkuInfoEntity> skuInfoEntities = skuInfoDao.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
        return skuInfoEntities;

    }

    @Override
    public SkuItemInfoVo getSkuItemInfoVoBySkuId(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemInfoVo skuItemInfoVo = new SkuItemInfoVo();


        //1. skuinfo  pms_sku_info
        CompletableFuture<SkuInfoEntity> future1 = CompletableFuture.supplyAsync(() -> {

            SkuInfoEntity skuInfoEntity = this.getById(skuId);
            skuItemInfoVo.setSkuInfoEntity(skuInfoEntity);
            return skuInfoEntity;
        }, poolExecutor);

        //2. skuimages  pms_sku_images
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> imagesBySkuId = skuImagesService.getSkuImagesBySkuId(skuId);
            skuItemInfoVo.setSkuImages(imagesBySkuId);
        }, poolExecutor);
        //3.销售属性
//    select DISTINCT pms_sku_sale_attr_value.attr_id,pms_sku_sale_attr_value.attr_name,pms_sku_sale_attr_value.attr_value
//    from pms_sku_sale_attr_value  LEFT JOIN pms_sku_info on pms_sku_sale_attr_value.sku_id = pms_sku_info.sku_id  where pms_sku_info.spu_id = 3
//
        CompletableFuture<Void> future3 = future1.thenAcceptAsync((skuInfoEntity) -> {
            List<SpuSaleAttrVo> spuSaleAttrVos = skuSaleAttrValueService.getSpuSaleAttrVosBySpuId(skuInfoEntity.getSpuId());
            skuItemInfoVo.setSpuSaleAttrVos(spuSaleAttrVos);
        }, poolExecutor);


        //4. 商品详细介绍  pms_spu_info_desc

        CompletableFuture<Void> future4 = future1.thenAcceptAsync((skuInfoEntity) -> {
            SpuInfoDescEntity descEntity = spuInfoDescService.getById(skuInfoEntity.getSpuId());
            skuItemInfoVo.setSpuInfoDesc(descEntity);
        }, poolExecutor);


        //5. 规格属性
//    select pms_product_attr_value.spu_id,pms_attr_group.attr_group_id,pms_attr_group.attr_group_name,pms_product_attr_value.attr_id,pms_product_attr_value.attr_name,pms_product_attr_value.attr_value
//    from pms_product_attr_value
//    LEFT JOIN pms_attr_attrgroup_relation on pms_product_attr_value.attr_id = pms_attr_attrgroup_relation.attr_id and pms_product_attr_value.spu_id=3
//    LEFT JOIN pms_attr_group on pms_attr_attrgroup_relation.attr_group_id =  pms_attr_group.attr_group_id
        CompletableFuture<Void> future5 = future1.thenAcceptAsync((skuInfoEntity) -> {
            List<SpuAttrGroupVo> spuAttrGroupVos =  productAttrValueService.getSpuAttrGroupVoBySpuId(skuInfoEntity.getSpuId());
            skuItemInfoVo.setSpuAttrGroupVos(spuAttrGroupVos);
        }, poolExecutor);


        CompletableFuture.allOf(future2,future3,future4,future5).get();


        return skuItemInfoVo;
    }

}